package org.jeecg.modules.lab.operation.controller;

import java.util.Arrays;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.cronJob.MyEnum;
import org.jeecg.common.cronJob.entity.LabCronJob;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.lab.cronJob.service.ILabCronJobService;
import org.jeecg.modules.lab.operation.entity.LabOperation;
import org.jeecg.modules.lab.operation.service.ILabOperationService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

/**
 * @Description: 单据修改日志表
 * @Author: jeecg-boot
 * @Date:   2023-08-14
 * @Version: V1.0
 */
@Api(tags="单据修改日志表")
@RestController
@RequestMapping("/operation/labOperation")
@Slf4j
public class LabOperationController extends JeecgController<LabOperation, ILabOperationService> {
	@Autowired
	private ILabOperationService labOperationService;

	@Autowired
	private ILabCronJobService labCronJobService;
	
	/**
	 * 分页列表查询
	 *
	 * @param labOperation
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "单据修改日志表-分页列表查询")
	@ApiOperation(value="单据修改日志表-分页列表查询", notes="单据修改日志表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<LabOperation>> queryPageList(LabOperation labOperation,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="50") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<LabOperation> queryWrapper = QueryGenerator.initQueryWrapper(labOperation, req.getParameterMap());
		Page<LabOperation> page = new Page<LabOperation>(pageNo, pageSize);
		IPage<LabOperation> pageList = labOperationService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param labOperation
	 * @return
	 */
	@AutoLog(value = "单据修改日志表-添加")
	@ApiOperation(value="单据修改日志表-添加", notes="单据修改日志表-添加")
//	@RequiresPermissions("operation:lab_operation:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody LabOperation labOperation) {
		labOperationService.save(labOperation);

		if (MyEnum.sync) {
			long time = new Date().getTime();
			LabCronJob job = new LabCronJob("lab_operation", "0", time, 1);
			labCronJobService.save(job);
		}
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param labOperation
	 * @return
	 */
	@AutoLog(value = "单据修改日志表-编辑")
	@ApiOperation(value="单据修改日志表-编辑", notes="单据修改日志表-编辑")
//	@RequiresPermissions("operation:lab_operation:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody LabOperation labOperation) {
		labOperation.setSynFlag("1");
		labOperationService.updateById(labOperation);

		if (MyEnum.sync) {
			long time = new Date().getTime();
			LabCronJob job = new LabCronJob("lab_operation", "1", time, 1);
			labCronJobService.save(job);
		}
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "单据修改日志表-通过id删除")
	@ApiOperation(value="单据修改日志表-通过id删除", notes="单据修改日志表-通过id删除")
//	@RequiresPermissions("operation:lab_operation:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		labOperationService.removeById(id);

		if (MyEnum.sync) {
			long time = new Date().getTime();
			String condition = "id='" + id + "'";
			LabCronJob job = new LabCronJob("lab_operation", "2", condition, time, 1);
			labCronJobService.save(job);
		}
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "单据修改日志表-批量删除")
	@ApiOperation(value="单据修改日志表-批量删除", notes="单据修改日志表-批量删除")
//	@RequiresPermissions("operation:lab_operation:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.labOperationService.removeByIds(Arrays.asList(ids.split(",")));

		if (MyEnum.sync) {
			long time = new Date().getTime();
			String[] idList = ids.split(",");
			labCronJobService.saveCronBatch("lab_operation","2",time,idList);
		}
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "单据修改日志表-通过id查询")
	@ApiOperation(value="单据修改日志表-通过id查询", notes="单据修改日志表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<LabOperation> queryById(@RequestParam(name="id",required=true) String id) {
		LabOperation labOperation = labOperationService.getById(id);
		if(labOperation==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(labOperation);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param labOperation
    */
//    @RequiresPermissions("operation:lab_operation:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, LabOperation labOperation) {
        return super.exportXls(request, labOperation, LabOperation.class, "单据修改日志表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
//    @RequiresPermissions("operation:lab_operation:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, LabOperation.class);
    }

}
